package com.quzi.kettle.activitise.main;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.LimitLine;
import com.github.mikephil.charting.utils.LimitLine.LimitLabelPosition;
import com.github.mikephil.charting.utils.XLabels;
import com.github.mikephil.charting.utils.XLabels.XLabelPosition;
import com.github.mikephil.charting.utils.YLabels;
import com.github.mikephil.charting.utils.YLabels.YLabelPosition;
import com.quzi.kettle.GlobalConfig;
import com.quzi.kettle.R;
import com.quzi.kettle.activitise.BaseActivity;
import com.quzi.kettle.activitise.added.SevenSea;
import com.quzi.kettle.activitise.added.WaterDateUtil;
import com.quzi.kettle.utils.MSharePerference;
import com.quzi.kettle.utils.ScreenShot;
import com.quzi.kettle.view.ITwoChoseButton;
import com.quzi.kettle.view.TitleBarHandler;
import com.quzi.kettle.view.TitleBarView;
import com.quzi.kettle.view.TwoChoseButton;

public class RecordActivity extends BaseActivity implements ITwoChoseButton {

	private TitleBarView titleView;
	private TwoChoseButton buttons;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_record);
		buttons = (TwoChoseButton) findViewById(R.id.selector);
		buttons.setOnLeftButtonClickListener(buttons.new MClickListener());
		buttons.setOnRightButtonClickListener(buttons.new MClickListener());
		buttons.setOnChooseListener(this);
		valuesDay = getDayValues();
		values = getSeven();
		mChart = (BarChart) findViewById(R.id.chart1);

		for (int i = 0; i < mDays.length; i++) {
			StringBuilder sb = new StringBuilder();
			if (i < 10) {
				sb.append("0" + i);
			} else {
				sb.append(i);
			}
			sb.append(":00");
			mDays[i] = sb.toString();
		}

		initTitleBar();
		initChart();
		loadBarChartData();
		loadBarChartData(mWeeks, 23, values);
	}

	protected BarChart mChart;

	private void initChart() {

		mChart.setUnit(" ");
		mChart.setDescription("");

		mChart.setDrawYValues(false);// 显示y轴数值

		// disable 3D
		mChart.set3DEnabled(false);
		// scaling can now only be done on x- and y-axis separately
		mChart.setPinchZoom(false);

		mChart.setDrawBarShadow(false);

		mChart.setDrawVerticalGrid(false);// 设置网格
		mChart.setDrawHorizontalGrid(true);
		mChart.setGridColor(Color.parseColor("#ffffff"));
		mChart.setDrawGridBackground(false);

		mChart.setMaxVisibleValueCount(7);
		mChart.setStartAtZero(true);
		mChart.setDoubleTapToZoomEnabled(false);

		XLabels xLabels = mChart.getXLabels();
		xLabels.setPosition(XLabelPosition.BOTTOM);
		xLabels.setCenterXLabelText(true);
		xLabels.setSpaceBetweenLabels(0);
		xLabels.setTextColor(getResources().getColor(R.color.white));

		YLabels y = mChart.getYLabels();
		y.setTextColor(Color.WHITE);
		y.setLabelCount(5);
		y.setPosition(YLabelPosition.LEFT);

		mChart.setDrawYLabels(true);
		mChart.setDrawLegend(false);

		mChart.setDrawBorder(true);
		mChart.setBorderColor(getResources().getColor(R.color.white));
		// add a nice and smooth animation
		mChart.invalidate();
		mChart.setHighlightEnabled(false);
		mChart.invalidate();
	}

	private String[] mDays = new String[24];
	private String[] mWeeks = com.quzi.kettle.utils.DateUtils.getWeekDay(7);
	private float[] values = { (float) 20, (float) 13, (float) 15, (float) 15,
			(float) 13, (float) 45, (float) 21 };
	private float[] valuesDay = new float[24];

	private float[] getSeven() {
		return WaterDateUtil.getSeven(getApplicationContext());

	}

	private float[] getDayValues() {
		String waterLevel = MSharePerference.getInstance(
				getApplicationContext()).get(GlobalConfig.WaterLevel);
		Log.d("kettle", "获取最近一天" + waterLevel);
		if (waterLevel.isEmpty()) {
			Log.v("Water", "waterLevel.isEmpty()---new float[24]");
			return new float[24];
		} else {
			// 解析吧
			float[] f = new float[24];
			String[] list = waterLevel.split(",");
			for (int i = 0; i < list.length; i++) {
				Log.v("Water", i+">>>>>>"+list[i]);
			}
			for (int i = 1; i < list.length; i++) {
				if (list[i].contains(":")) {

					String[] pair = list[i].split(":");
					Log.v("Water",
							"Integer.valueOf(pair[0])----"
									+ Integer.valueOf(pair[0])
									+ "Integer.valueOf(pair[1])---"
									+ Integer.valueOf(pair[1]));
					f[Integer.valueOf(pair[0])] = Integer.valueOf(pair[1]);

				}
			}
			return f;
		}
	}

	/**
	 * 加载空数据
	 */
	private void loadBarChartData() {

		ArrayList<String> xVals = new ArrayList<String>();
		xVals.add(" " + mChart.getUnit());

		ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
		BarDataSet set1 = new BarDataSet(yVals1, "Data Set");
		// set1.setColor(getResources().getColor(R.color.halfwhite));
		ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
		dataSets.add(set1);

		BarData data = new BarData(xVals, dataSets);

		mChart.clear();
		mChart.setData(data);
		mChart.notifyDataSetChanged();
	}

	private void loadBarChartData(String[] mainData, int limited, float[] vals) {
		// y轴数值
		ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
		for (int i = 0; i < vals.length; i++) {
			float val1 = vals[i];
			yVals1.add(new BarEntry(val1, i));
		}

		// x轴文字
		ArrayList<String> xVals = new ArrayList<String>();
		for (int i = 0; i < mainData.length; i++) {
			xVals.add(mainData[i] + " " + mChart.getUnit());
		}

		BarDataSet set1 = new BarDataSet(yVals1, "Data Set");
		set1.setColor(Color.parseColor("#88ffffff"));
		ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
		dataSets.add(set1);

		BarData data = new BarData(xVals, dataSets);

		LimitLine limitLine = new LimitLine(limited);
		limitLine.setDrawValue(true);
		limitLine.setLabelPosition(LimitLabelPosition.LEFT);
		limitLine.setLineWidth(2);
		limitLine.setLabelPosition(LimitLabelPosition.RIGHT);
		// data.addLimitLine(limitLine);
		mChart.clear();
		mChart.setData(data);
		mChart.notifyDataSetChanged();
		mChart.animateY(1000);
	}

	@Override
	protected void initTitleBar() {
		// TODO Auto-generated method stub
		super.initTitleBar();
		titleView = (TitleBarView) findViewById(R.id.titleview);
		titleView.setTitle(getResources().getString(R.string.record));
		titleView.setRightText("分享");
		titleView.setRightTextColor(getResources().getColor(R.color.pink));
		titleView.setTitleBarClickListener(new TitleBarHandler() {

			@Override
			public void ClickRight() {
				// TODO Auto-generated method stub
				Log.d("share", "OnRightClick!!!");
				ScreenShot.shoot(RecordActivity.this,
						Environment.getExternalStorageDirectory()
								+ "/IMAGE.jpeg");
				Intent intent = new Intent(Intent.ACTION_SEND);

				File f = new File(Environment.getExternalStorageDirectory()
						+ "/IMAGE.jpeg");
				if (f != null && f.exists() && f.isFile()) {
					intent.setType("image/jpg");
					Uri u = Uri.fromFile(f);
					intent.putExtra(Intent.EXTRA_STREAM, u);
					intent.putExtra(Intent.EXTRA_TEXT, "zzzz");
					intent.putExtra(Intent.EXTRA_SUBJECT, "aaaa");
					intent.putExtra(Intent.EXTRA_TITLE, "bbbb");
				}
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(Intent.createChooser(intent, getTitle()));
			}

			@Override
			public void ClickLeft() {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
	}

	@Override
	public void ClickLeft() {
		// TODO Auto-generated method stub
		loadBarChartData(mWeeks, 30, values);
	}

	@Override
	public void ClickRight() {
		// TODO Auto-generated method stub
		loadBarChartData(mDays, 60, valuesDay);
	}

}
